﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.AspNet.SignalR.Client;

namespace WcfBusinessLogic.Model
{
    public static class HubCommunication
    {
        static string clientURL = Properties.Settings.Default.ClientURL;
        static HubConnection aHubConnection = new HubConnection(clientURL);
        static IHubProxy aChatHubProxy = aHubConnection.CreateHubProxy("ChatHub");
        static bool started = false;
        public static void broadcast(string name, string message, string target)
        {
            startConn();
            object[] MyArguments = new object[3] { name, message, target };
            aChatHubProxy.Invoke("Broadcast", MyArguments);
            //aHubConnection.Stop();
        }
        public static void sendTo(string charID, string message, string target)
        {
            startConn();
            object[] MyArguments = new object[3] { charID, message, target };
            aChatHubProxy.Invoke("SendTo", MyArguments);
        }
        public static void disconnectCharacter(string charID)
        {
            startConn();
            object[] MyArguments = new object[1] { charID };
            aChatHubProxy.Invoke("CharDisconnect", MyArguments);
        }
        private static void startConn()
        {
            if(started == false)
            {
                aHubConnection.Start().Wait();
                started = true; 
            }
        }
        private static void stopConn()
        {
            if (started == true)
            {
                aHubConnection.Stop();
                started = false;
            }
        }
    }
}